<?php
/**
 * Created by PhpStorm.
 * User: ouryun
 * Date: 2021/5/12
 * Time: 11:07
 */

namespace app\api\controller;


use app\api\logic\FaceRecognitionLogic;
use think\admin\Controller;
use think\App;

/**
 * 人脸识别
 * Class FaceRecognition
 * @package app\api\controller
 */
class FaceRecognition extends Controller
{
    protected $_datas;
    protected $_logic;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->_logic = new FaceRecognitionLogic();
    }

    /**
     * 获取人脸识别数据
     * @return \think\response\Json
     */
    public function getFaceDataAction()
    {
        $this->_datas = $this->request->param();
        if (!isset($this->_datas['data'])) {
            return returnJson(HTTP_CODE_ERROR, '数据不能为空', '');
        }
        $data = $this->_datas['data'];
        $faceId = $data['face_id'] ?? '';
        if (empty($faceId)) {
            return returnJson(HTTP_CODE_ERROR, 'face_id不能为空', '');
        }
        $device_id = $data['device_id'];
        $channel_id = $data['channel_id'];
        if (empty($device_id) || $channel_id === '') {
            return returnJson(HTTP_CODE_ERROR, '设备id不能为空', '');
        }
        $result = $this->_logic->getFaceData($data);
        if (!$result) {
            return returnJson(HTTP_CODE_ERROR, '失败', '');
        }
        return returnJson(HTTP_CODE_SUCCESS, '成功', '');
    }

    /**
     * 获取设备异常信息
     * @return \think\response\Json
     */
    public function getDeviceStatusAction()
    {
        $this->_datas = $this->request->param();
        if (!isset($this->_datas['data'])) {
            return returnJson(HTTP_CODE_ERROR, '数据不能为空', '');
        }
        $method = $this->_datas['data']['method'] ?? [];
        if (empty($method)) {
            return returnJson(HTTP_CODE_ERROR, '无法识别的方法', '');
        }
        $data = $this->_datas['data']['param'] ?? [];
        if (empty($data)) {
            return returnJson(HTTP_CODE_ERROR, '数据不能为空', '');
        }
        $data['method'] = $method;
        $result = $this->_logic->getDeviceStatus($data);
        if (!$result) {
            return returnJson(HTTP_CODE_ERROR, '失败', '');
        }
        return returnJson(HTTP_CODE_SUCCESS, '成功', '');
    }

}